package com.campuscarbooking.api.service.impl;

import com.campuscarbooking.api.annotation.LogExecutionTime;
import com.campuscarbooking.api.annotation.LogMethod;
import com.campuscarbooking.api.dto.message.ReservationNotificationMessage;
import com.campuscarbooking.api.dto.message.ScheduleUpdateMessage;
import com.campuscarbooking.api.model.Reservation;
import com.campuscarbooking.api.model.BusSchedule;
import com.campuscarbooking.api.model.User;
import com.campuscarbooking.api.service.MessageService;
import com.campuscarbooking.api.service.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * Implementation of NotificationService using MessageService for sending notifications via RabbitMQ
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationServiceImpl implements NotificationService {

    private final MessageService messageService;

    @Override
    @LogMethod("创建预约通知")
    @LogExecutionTime
    public void notifyReservationCreated(Reservation reservation) {
        log.info("Creating notification for new reservation ID: {}", reservation.getId());
        sendReservationNotification(reservation, "CREATED");
    }

    @Override
    @LogMethod("确认预约通知")
    @LogExecutionTime
    public void notifyReservationConfirmed(Reservation reservation) {
        log.info("Creating notification for confirmed reservation ID: {}", reservation.getId());
        sendReservationNotification(reservation, "CONFIRMED");
    }

    @Override
    @LogMethod("取消预约通知")
    @LogExecutionTime
    public void notifyReservationCancelled(Reservation reservation) {
        log.info("Creating notification for cancelled reservation ID: {}", reservation.getId());
        sendReservationNotification(reservation, "CANCELLED");
    }

    @Override
    @LogMethod("发送预约提醒")
    @LogExecutionTime
    public void sendReservationReminder(Reservation reservation) {
        log.info("Creating reminder notification for reservation ID: {}", reservation.getId());
        sendReservationNotification(reservation, "REMINDER");
    }

    @Override
    @LogMethod("班车时刻表更新通知")
    @LogExecutionTime
    public void notifyScheduleUpdated(BusSchedule schedule, String updateType) {
        log.info("Creating notification for schedule update: {} - {}", schedule.getId(), updateType);

        // Create the schedule update message
        ScheduleUpdateMessage message = ScheduleUpdateMessage.builder()
                .scheduleId(schedule.getId())
                .departureTime(schedule.getDepartureTime())
                .arrivalTime(schedule.getArrivalTime())
                .routeDirection(schedule.getRouteDirection().name())
                .status(schedule.getStatus().name())
                .updateType(updateType)
                .price(schedule.getPrice())
                .capacity(schedule.getCapacity())
                .build();

        // Send the message
        messageService.sendScheduleUpdate(message);
    }

    @LogMethod("发送预约通知")
    private void sendReservationNotification(Reservation reservation, String notificationType) {
        BusSchedule schedule = reservation.getSchedule();
        User user = reservation.getUser();

        // Create the notification message
        ReservationNotificationMessage message = ReservationNotificationMessage.builder()
                .reservationId(reservation.getId())
                .userId(user.getId())
                .userName(user.getNickname())
                .phoneNumber(user.getPhone())
                .userType(user.getUserType().name())
                .scheduleId(schedule.getId())
                .departureTime(schedule.getDepartureTime())
                .arrivalTime(schedule.getArrivalTime())
                .routeDirection(schedule.getRouteDirection().name())
                .reservationStatus(reservation.getStatus().name())
                .notificationType(notificationType)
                .price(reservation.getAmountPaid())
                .seatNumber(reservation.getSeatNumber())
                .build();

        // Send the message
        messageService.sendReservationNotification(message);
    }
} 